package com.example.lenovo.myulbumdemo;

import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;

public class VideoActivity extends AppCompatActivity {
    private Button btn_selectVideo;//选择视频
    private Button btn_preVideo;//预览视频
    private boolean canPlay;
    private ArrayList<AlbumFile> mAlbumFiles;
    private Uri videoUri;
    private VideoView vv_video;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        btn_selectVideo = findViewById(R.id.btn_selectVideo);
        btn_preVideo = findViewById(R.id.btn_preVideo);
        vv_video = findViewById(R.id.vv_video);
        btn_selectVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectVideo();
            }
        });
        btn_preVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (canPlay == true) {
                    vv_video.setVideoURI(videoUri);
                    vv_video.start();
                }

            }
        });
    }

    private void selectVideo() {
        Album.video(this)
                .multipleChoice()//支持多选或
                .requestCode(200)//
                .columnCount(2)//两列
                .selectCount(6)//可以选六张
                .camera(true)//页面当中有拍摄图标
                .checkedList(mAlbumFiles)
                .onResult(new Action<ArrayList<AlbumFile>>() {
                    @Override
                    public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                        Log.e("TAG", " =====()" + result.get(0).getPath());
                        String path = result.get(0).getPath();
                        String thumbPath = result.get(0).getThumbPath();//预览图
                        long size = result.get(0).getSize() / 1000000;
                        Log.e("TAG", " =====()视频的大小是" + size);
                        videoUri = Uri.parse(path);
                        canPlay = true;
                        postArtical(path, thumbPath);

                    }
                })
                .onCancel(new Action<String>() {
                    @Override
                    public void onAction(int requestCode, @NonNull String result) {

                    }
                })
                .start();
    }

    private void postArtical(String path, String thumbPath) {
        HttpParams params = new HttpParams();
        params.put("userid", 1);
        params.put("cid", 2);
        params.put("title", "你好");
        params.put("content", "这是文章内容");
        Log.e("TAG", " =====()" + path + "缩略图的地址是" + thumbPath);
        File videoFile = new File(path);
        File videoThumbFile = new File(thumbPath);


        params.put("video_file", videoFile);
        params.put("video_pic", videoThumbFile);
        Log.e("TAG", " =====()" + params.toString());
        OkGo.<String>post(Urls.postArtical)
                .params(params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("TAG", "联网成功 =====()" + response.body());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        Log.e("TAG", "联网失败 =====()");
                        super.onError(response);
                    }
                });
    }
}
